home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / examples / c / texobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  4.2 KB  |  194 lines

  1. /*
  2. * Example of using the 1.1 texture object functions.
  3. * Also, this demo utilizes Mesa's fast texture map path.
  4. *
  5. * Brian Paul   June 1996
  6. */
  7.  
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include <ad709/tinygl/glut.h> 
  14.  
  15. static GLuint TexObj[2];
  16. static GLfloat Angle = 0.0f;
  17.  
  18. static int cnt=0,v=0;
  19. int w=320,h=240;
  20.  
  21. void draw(void) {
  22.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  23.     
  24.     glColor3f(1.0, 1.0, 1.0);
  25.     
  26.     /* draw first polygon */
  27.     glPushMatrix();
  28.     glTranslatef(-1.0, 0.0, 0.0);
  29.     glRotatef(Angle, 0.0, 0.0, 1.0);
  30.     glBindTexture(GL_TEXTURE_2D, TexObj[v]);
  31.     
  32.     glEnable(GL_TEXTURE_2D);
  33.     glBegin(GL_QUADS);
  34.     glTexCoord2f(0.0, 0.0);
  35.     glVertex2f(-1.0, -1.0);
  36.     glTexCoord2f(1.0, 0.0);
  37.     glVertex2f(1.0, -1.0);
  38.     glTexCoord2f(1.0, 1.0);
  39.     glVertex2f(1.0, 1.0);
  40.     glTexCoord2f(0.0, 1.0);
  41.     glVertex2f(-1.0, 1.0);
  42.     glEnd();
  43.     glDisable(GL_TEXTURE_2D);
  44.     glPopMatrix();
  45.     
  46.     /* draw second polygon */
  47.     glPushMatrix();
  48.     glTranslatef(1.0, 0.0, 0.0);
  49.     glRotatef(Angle - 90.0, 0.0, 1.0, 0.0);
  50.     
  51.     glBindTexture(GL_TEXTURE_2D, TexObj[1-v]);
  52.     
  53.     glEnable(GL_TEXTURE_2D);
  54.     glBegin(GL_QUADS);
  55.     glTexCoord2f(0.0, 0.0);
  56.     glVertex2f(-1.0, -1.0);
  57.     glTexCoord2f(1.0, 0.0);
  58.     glVertex2f(1.0, -1.0);
  59.     glTexCoord2f(1.0, 1.0);
  60.     glVertex2f(1.0, 1.0);
  61.     glTexCoord2f(0.0, 1.0);
  62.     glVertex2f(-1.0, 1.0);
  63.     glEnd();
  64.     glDisable(GL_TEXTURE_2D);
  65.     
  66.     glPopMatrix();
  67. }
  68.  
  69.  
  70. /* new window size or exposure */
  71. void reshape(int width, int height) {
  72.     glViewport(0, 0, (GLint) width, (GLint) height);
  73.     glMatrixMode(GL_PROJECTION);
  74.     glLoadIdentity();
  75.     /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 ); */
  76.     glFrustum(-2.0, 2.0, -2.0, 2.0, 6.0, 20.0);
  77.     glMatrixMode(GL_MODELVIEW);
  78.     glLoadIdentity();
  79.     glTranslatef(0.0, 0.0, -8.0);
  80. }
  81.  
  82.  
  83. void bind_texture(int texobj,int image)
  84. {
  85.     static int width = 8, height = 8;
  86.     static int color[2][3]={
  87.         {255,0,0},
  88.         {0,255,0},
  89.     };
  90.     GLubyte tex[64][3];
  91.     static GLubyte texchar[2][8*8] = {
  92.         {
  93.             0, 0, 0, 0, 0, 0, 0, 0,
  94.                 0, 0, 0, 0, 1, 0, 0, 0,
  95.                 0, 0, 0, 1, 1, 0, 0, 0,
  96.                 0, 0, 0, 0, 1, 0, 0, 0,
  97.                 0, 0, 0, 0, 1, 0, 0, 0,
  98.                 0, 0, 0, 0, 1, 0, 0, 0,
  99.                 0, 0, 0, 1, 1, 1, 0, 0,
  100.                 0, 0, 0, 0, 0, 0, 0, 0},
  101.             {
  102.                 0, 0, 0, 0, 0, 0, 0, 0,
  103.                     0, 0, 0, 2, 2, 0, 0, 0,
  104.                     0, 0, 2, 0, 0, 2, 0, 0,
  105.                     0, 0, 0, 0, 0, 2, 0, 0,
  106.                     0, 0, 0, 0, 2, 0, 0, 0,
  107.                     0, 0, 0, 2, 0, 0, 0, 0,
  108.                     0, 0, 2, 2, 2, 2, 0, 0,
  109.                     0, 0, 0, 0, 0, 0, 0, 0}};
  110.                 
  111.                 int i,j;
  112.                 
  113.                 glBindTexture(GL_TEXTURE_2D, texobj);
  114.                 
  115.                 /* red on white */
  116.                 for (i = 0; i < height; i++) {
  117.                     for (j = 0; j < width; j++) {
  118.                         int p = i * width + j;
  119.                         if (texchar[image][(height - i - 1) * width + j]) {
  120.                             tex[p][0] = color[image][0];
  121.                             tex[p][1] = color[image][1];
  122.                             tex[p][2] = color[image][2];
  123.                         } else {
  124.                             tex[p][0] = 255;
  125.                             tex[p][1] = 255;
  126.                             tex[p][2] = 255;
  127.                         }
  128.                     }
  129.                 }
  130.                 glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
  131.                     GL_RGB, GL_UNSIGNED_BYTE, tex);
  132.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  133.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  134.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  135.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  136.                 /* end of texture object */
  137. }
  138.  
  139.  
  140.  
  141. void init(void) {
  142.     glEnable(GL_DEPTH_TEST);
  143.     
  144.     /* Setup texturing */
  145.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  146.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  147.     
  148.     /* generate texture object IDs */
  149.     glGenTextures(2, TexObj);
  150.     bind_texture(TexObj[0],0);
  151.     bind_texture(TexObj[1],1);
  152. }
  153.  
  154.  
  155. void idle(void) {
  156.     Angle += 2.0;
  157.     if (++cnt==50) {
  158.         cnt=0;
  159.     //    v=!v;
  160.     }
  161.     draw();
  162.     glutPostRedisplay();
  163. }
  164.  
  165. /* change view angle, exit upon ESC */
  166. GLenum key(int k, GLenum mask)
  167. {
  168.     switch (k) {
  169.     case 'q':
  170.     case 27:
  171.         glutDestroyWindow(0);
  172.         exit(0);
  173.     }
  174.     return GL_FALSE;
  175. }
  176.  
  177. int main(int argc, char **argv)    {
  178.     glutInit(&argc, argv);
  179.     glutInitWindowSize(w, h);
  180.     glutInitWindowPosition(0, 0);
  181.     
  182.     glutCreateWindow("Texture-mapped object");
  183.     init();
  184.  
  185.     glutDisplayFunc(draw);
  186.     glutReshapeFunc(reshape);
  187.     glutIdleFunc(idle);
  188.     glutMainLoop();
  189.  
  190.     return 0;
  191. }
  192.  
  193.  
  194.